#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";

// 添加报头
std::string EnCode(std::string &body)
{
    int len = body.size();
    std::string package = std::to_string(len) + protocol_sep + body + protocol_sep;
    return package;
}
// 去报头 "len" \n "body" \n.....
bool DeCode(std::string &package, std::string &body)
{
    size_t pos = package.find(protocol_sep);
    if (pos == std::string::npos)
        return false;
    std::string len_str = package.substr(0, pos);
    int len = std::stoi(len_str);
    int total_len = len_str.size() + len + 2 * protocol_sep.size();
    if (package.size() < total_len)
        return false;
    body = package.substr(pos + protocol_sep.size(), len);
    // 删除报文
    package.erase(0, total_len);
    return true;
}
class Request
{
public:
    Request() = default;
    Request(int x, int y, char op)
        : x_(x), y_(y), op_(op)
    {
    }

public:
    bool Serialize(std::string &out)
    {
#ifdef MySelf
        // 构建报文的有效载荷
        // struct => string, "x op y"
        std::string s = std::to_string(x_);
        s += blank_space_sep;
        s += op_;
        s += blank_space_sep;
        s += std::to_string(y_);
        out = s;
        return true;
#else
        Json::Value root;
        root["x"] = x_;
        root["y"] = y_;
        root["op"] = op_;
        Json::FastWriter w;
        out = w.write(root);
        return true;
#endif
    }

    bool DeSerialize(const std::string &in)
    {
#ifdef MySelf
        std::size_t left = in.find(blank_space_sep);
        if (left == std::string::npos)
            return false;
        std::string part_x = in.substr(0, left);

        std::size_t right = in.rfind(blank_space_sep);
        if (right == std::string::npos)
            return false;
        std::string part_y = in.substr(right + 1);

        if (left + 2 != right)
            return false;
        op_ = in[left + 1];
        x_ = std::stoi(part_x);
        y_ = std::stoi(part_y);
        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);
        x_ = root["x"].asInt();
        y_ = root["y"].asInt();
        op_ = root["op"].asInt();
        return true;
#endif
    }

public:
    int x_;
    int y_;
    char op_;
};

class Response
{
public:
    Response() = default;
    Response(int result, int code)
        : result_(result), code_(code)
    {
    }

public:
    bool Serialize(std::string &out)
    {
#ifdef MySelf
        // "result code"
        // 构建报文的有效载荷
        std::string s = std::to_string(result_);
        s += blank_space_sep;
        s += std::to_string(code_);
        out = s;
        return true;
#else

        Json::Value root;
        root["result"] = result_;
        root["code"] = code_;
        Json::FastWriter w;
        out = w.write(root);
        return true;
#endif
    }

    bool DeSerialize(const std::string &in)
    {
#ifdef MySelf
        std::size_t pos = in.find(blank_space_sep);
        if (pos == std::string::npos)
            return false;
        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos + 1);

        result_ = std::stoi(part_left);
        code_ = std::stoi(part_right);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);
        result_ = root["result"].asInt();
        code_ = root["code"].asInt();
        return true;
#endif
    }

    void DebugPrint()
    {
        std::cout << "结果响应完成, result: " << result_ << ", code: "<< code_ << std::endl;
    }
public:
    int result_;
    int code_;
};